package com.iot.manager.ui.activities;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.iot.manager.R;
import com.iot.manager.data.models.Device;
import com.iot.manager.network.ApiService;
import com.iot.manager.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditDeviceActivity extends AppCompatActivity {
    private String deviceId;
    private ApiService apiService;
    private EditText etDeviceName;
    private EditText etDeviceType;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_device);

        deviceId = getIntent().getStringExtra("deviceId");
        if (deviceId == null) {
            finish();
            return;
        }

        setupToolbar();
        initViews();
        loadDeviceData();
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(R.string.edit_device);
        }
    }

    private void initViews() {
        etDeviceName = findViewById(R.id.et_device_name);
        etDeviceType = findViewById(R.id.et_device_type);
        btnSave = findViewById(R.id.btn_save);

        btnSave.setOnClickListener(v -> saveDevice());
    }

    private void loadDeviceData() {
        apiService = RetrofitClient.getInstance().getApiService();
        apiService.getDevice(deviceId).enqueue(new Callback<Device>() {
            @Override
            public void onResponse(Call<Device> call, Response<Device> response) {
                if (response.isSuccessful() && response.body() != null) {
                    Device device = response.body();
                    etDeviceName.setText(device.getName());
                    etDeviceType.setText(device.getType());
                } else {
                    Toast.makeText(EditDeviceActivity.this, R.string.error_loading_device, Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFailure(Call<Device> call, Throwable t) {
                Toast.makeText(EditDeviceActivity.this, R.string.network_error, Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    private void saveDevice() {
        String name = etDeviceName.getText().toString().trim();
        String type = etDeviceType.getText().toString().trim();

        if (name.isEmpty()) {
            etDeviceName.setError(getString(R.string.error_device_name_required));
            return;
        }

        Device device = new Device();
        device.setName(name);
        device.setType(type);

        apiService.updateDevice(deviceId, device).enqueue(new Callback<Device>() {
            @Override
            public void onResponse(Call<Device> call, Response<Device> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(EditDeviceActivity.this, R.string.device_updated, Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(EditDeviceActivity.this, R.string.error_updating_device, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Device> call, Throwable t) {
                Toast.makeText(EditDeviceActivity.this, R.string.network_error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 